/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package czStvDpds.graphics3D.gui;

import czStvDpds.graphics3D.GraphicsIndex;
import czStvDpds.graphics3D.TextureCreator;
import czStvDpds.graphics3D.geometry.Texture;
import czStvDpds.graphics3D.geometry.TextureProperties;
import czStvDpds.input.LWJGLKeyEvent;
import czStvDpds.logging.Logging;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import javax.imageio.ImageIO;
import org.lwjgl.input.Mouse;

/**
 *
 * @author Ondra
 */
public class LCheckbox extends LComponent {

    private LCheckboxGroup group;
    private boolean state;
    private String text;
    private int textX, textY;
    private ArrayList<CheckboxListener> listeners;
    private Texture basic, selected;
    private static final int OUTLINE = 7;

    public LCheckbox(String text, boolean state) {
        super();
        super.model = new GUIObject(Modeler.generateCheckboxMesh(loc.getWidth(), loc.getHeight()));
        this.state = state;
        listeners = new ArrayList();

        setText(text);

        createTextures();
        super.model.setTexture(state ? selected : basic);
        recalculateMinSize();
    }

    public LCheckbox(String text) {
        this(text, false);
    }

    private void createTextures() {
        basic = new Texture();
        selected = new Texture();
        BufferedImage imgBasic = null;
        BufferedImage imgSelected = null;
        try {
            imgBasic = ImageIO.read(new File("resources/models/gui-textures/checkbox-default.png"));
            imgSelected = ImageIO.read(new File("resources/models/gui-textures/checkbox-selected.png"));
        } catch (IOException ex) {
            Logging.getMainLogger().log(Level.WARNING, "Failed to load texture: GUI.LCheckbox, because of {0}", ex.getLocalizedMessage());
        }
        TextureProperties prop = new TextureProperties();
        prop.setType(TextureProperties.TEXTURE_2D);
        prop.setWidth(4);
        prop.setHeight(1);
        prop.setFilterMax(TextureProperties.FILTER_NEAREST);
        basic.setProperties(prop);
        basic.setData(TextureCreator.getImageData(imgBasic));
        selected.setProperties(prop);
        selected.setData(TextureCreator.getImageData(imgSelected));

        GraphicsIndex.getGLSynchronizer().generateTexture(basic);
        GraphicsIndex.getGLSynchronizer().generateTexture(selected);
    }

    /**
     * prida policku posluchac udalosti. Posluchac udalosti dostava do sve
     * metody checkboxStateChanged() informace o zmenach stavu policka
     *
     * @param newListener novy posluchac
     */
    public void addCheckboxListener(CheckboxListener newListener) {
        listeners.add(newListener);
    }

    /**
     * odebere dany posluchac ze seznamu posluchacu
     *
     * @param listener posluchac, ktery ma byt odebran
     */
    public void removeCheckboxListener(CheckboxListener listener) {
        listeners.remove(listener);
    }

    @Override
    protected void model() {

        this.model.setPosition(loc.getX() + Modeler.pixelsToRatio(offset), loc.getY() + Modeler.pixelsToRatio(offset));
//        this.model.setText(text);
        this.model.setFont(font);
        //stanovovani pozice textu
        textX = (2 * OUTLINE) + Modeler.ratioToPixels(loc.getHeight());
        textY = ((Modeler.ratioToPixels(loc.getHeight()) - font.getHeight(text)) / 2);
//        this.model.setTextX(textX);
//        this.model.setTextY(textY);
        model.setText(TextPiece.cut(text, textX, textY, font));
        this.model.resize(Modeler.resizeCheckboxMesh(loc.getWidth() - (Modeler.pixelsToRatio(offset) * 2), loc.getHeight() - (Modeler.pixelsToRatio(offset) * 2)));
    }

    /**
     * nastavi text komponenty a podle toho i jeji velikost
     *
     * @param text
     */
    public final void setText(String text) {
        this.text = text;
//        this.model.setText(text);
        model.setText(TextPiece.cut(text, textX, textY, font));
        recalculateMinSize();
    }

    /**
     * ocividne vraci text komponenty
     *
     * @return text komponenty
     */
    public String getText() {
        return text;
    }

    /**
     * vraci hodnotu policka
     *
     * @return true - zaskrtnuto, false - nezaskrtnuto
     */
    public boolean getState() {
        return state;
    }

    /**
     * nastavi hodnotu polika. Toto NEVYVOLA udalost! (male vylepseni oproti
     * swingu :D)
     *
     * @param state true - zaskrtnuto, false - nezaskrtnuto
     */
    public void setState(boolean state) {
        this.state = state;
        model.setTexture(state ? selected : basic);
    }

    public ArrayList<CheckboxListener> getListeners() {
        return listeners;
    }

    public void setListeners(ArrayList<CheckboxListener> listeners) {
        this.listeners = listeners;
    }

    /**
     * zde je provadena akce po zmacknuti mysi (zmena stavu)
     */
    @Override
    protected void mousePressed() {
        state = !state;
        for (CheckboxListener listener : listeners) {
            listener.checkboxStateChanged(new CheckboxEvent(this, state, Mouse.getX(), Mouse.getY()));
        }
        model.setTexture(state ? selected : basic);
        System.out.println("checkbox kliknut, novy stav= "+state);
    }

    @Override
    protected final void recalculateMinSize() {
        int textHeightPx = font.getHeight(text);
        int textWidthPx = font.getWidth(text);

        int minHeightPx = textHeightPx + (2 * OUTLINE);
        int minWidthPx = textWidthPx + (3 * OUTLINE) + textHeightPx/*kvuli ctverci*/;

        setMinSize(new ComponentPixelSize(minHeightPx, minWidthPx));

        if (parent != null) {
            parent.childChanged(this);
        }
    }

    @Override
    protected void mouseExited() {
    }

    @Override
    protected void mouseEntered() {
    }

    @Override
    protected void mouseReleased() {
    }

    @Override
    protected void keyPressed(LWJGLKeyEvent e) {
    }

    @Override
    protected void mousePressedNotHere() {
    }
}
